package com.songkun.template.ui.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import com.songkun.template.config.AppApplication;
import com.songkun.template.config.AppController;
import com.songkun.template.databinding.ActivityBaseBinding;
import com.songkun.template.utils.eventObj.VoidEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;


/**
 * @描述: 所有Fragment的基类
 * @项目名: AndroidTemplate
 * @包名: com.songkun.template.ui.base
 * @类名:
 * @作者: soongkun
 * @创建时间: 2016/4/29 13:16
 */

public class BaseFragment extends Fragment {

    private BaseActivity mActivity;
    private AppController mController;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActivity = (BaseActivity) getActivity();
        mController = AppController.getInstance();

        //注册数据事件
        if(!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }
    }


    /**
     * 获取首页baseBinding
     * @return
     */
    protected ActivityBaseBinding getActivityBaseViewBinding(){
        return ((CommonActivity)getActivity()).getActivityBaseViewBinding();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        //监测内存泄露
        AppApplication.getRefWatcher(mActivity).watch(this);

        //反注册事件
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe
    public void onInvalid(VoidEvent event){

    }
}
